package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.FileLabelResponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.SelectFileLabelView;
import com.topboya.linker.ui.view.SelectFileView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SelectFileLabelPresenter extends BasePresenter<SelectFileLabelView> {

    private static final String TAG = "SelectFileLabelPresenter:";
    public SelectFileLabelPresenter(BaseActivity context) {
        super(context);
    }

    public void getProductFileLabelList(String type){

            mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
            ApiRetrofit.getInstance().getFilelableByDicKey(type)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(fileLabelResponse -> {
                        String success = fileLabelResponse.getMeta().getSuccess();
                        mContext.hideWaitingDialog();

                        if (null != success && Boolean.parseBoolean(success)) {

                            List<FileLabelResponse.Data> fileLists = fileLabelResponse.getData();
                             getView().getFileLableList( fileLists);
                        }

                    }, this::loginError);
        }

    public void getProductFileList(String productId,String fileType){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getProductFileList(productId,fileType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(selectfileresponse -> {
                    String success = selectfileresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {

                        List<SelectFileResponse.FileList> fileLists = selectfileresponse.getData().getList();
                        getView().getProductFileList( fileLists);
                    }

                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }
}
